package com.spider.spiderflowable.business.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spider.spiderflowable.business.entity.model.FlowApprovalRequest;
import com.spider.spiderflowable.business.entity.param.ApprovalParam;
import com.spider.spiderflowable.business.entity.param.ApprovalQueryParam;
import com.spider.spiderflowable.business.entity.param.ApprovalRequestSubmitParam;
import com.spider.spiderflowable.business.entity.vo.FlowApprovalRequestVo;
import com.spider.spiderflowable.business.enums.RequestTypeEnum;
import com.spider.spiderflowable.business.service.ApprovalRequestService;
import com.spider.spiderflowable.common.ResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiong.bo
 * @version 1.0
 * @date 2025/4/20 3:09 下午
 */

@Slf4j
@Tag(name = "审批请求相关接口管理")
@RequestMapping("/api/approval")
@RestController
public class ApprovalRequestController {

  @Autowired
  private ApprovalRequestService approvalRequestService;

  @Operation(summary = "提交工单异常关闭申请")
  @PostMapping("/abnormal-close/submit")
  public ResponseEntity<FlowApprovalRequest> submitApply(@Validated @RequestBody ApprovalRequestSubmitParam param) {
    log.info("提交工单异常关闭申请,submitApply param:{}", JSON.toJSONString(param));
    param.setRequestType(RequestTypeEnum.REQUEST_ABNORMAL_CLOSE.getCode());
    param.setRequestTypeName(RequestTypeEnum.REQUEST_ABNORMAL_CLOSE.getName());
    return ResponseEntity.success(approvalRequestService.submitApply(param));
  }

  @Operation(summary = "审批通过")
  @PostMapping("/approve")
  public ResponseEntity<FlowApprovalRequest> approveRequest(@Validated @RequestBody ApprovalParam param) {
    log.info("审批通过,approveRequest param:{}", JSON.toJSONString(param));
    return ResponseEntity.success(approvalRequestService.approveRequest(param));
  }

  @Operation(summary = "审批驳回")
  @PostMapping("/reject")
  public ResponseEntity<FlowApprovalRequest> rejectRequest(@Validated @RequestBody ApprovalParam param) {
    log.info("审批驳回,rejectRequest param:{}", JSON.toJSONString(param));
    return ResponseEntity.success(approvalRequestService.rejectRequest(param));
  }

  @Operation(summary = "审批列表查询")
  @GetMapping
  public ResponseEntity<IPage<FlowApprovalRequestVo>> selectPage(ApprovalQueryParam param) {
    log.info("审批列表查询,selectPage param:{}", JSON.toJSONString(param));
    return ResponseEntity.success(approvalRequestService.selectPage(param));
  }

  @Operation(summary = "审批详情")
  @GetMapping("/{id}")
  public ResponseEntity<FlowApprovalRequestVo> findById(@PathVariable(name="id") Long id) {
    return ResponseEntity.success(approvalRequestService.findById(id));
  }



}
